package reports;

import java.io.IOException;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import fj.F;

import server.Initialize;
import server.db.DatabaseFunctor;
import server.db.StmtHelper;
import server.objects.Photo;

import ajaxmethods.AjaxMethod;

public class DetailReport implements AjaxMethod {

	Map<String,String> listingMap = new HashMap<String,String>();
	
	private F<List<Map<String, String>>, DatabaseFunctor<List<String>>> getPhotos = new F<List<Map<String, String>>, DatabaseFunctor<List<String>>>() {

		@Override
		public DatabaseFunctor<List<String>> f(List<Map<String, String>> arg0) {
			listingMap = arg0.get(0);
			return StmtHelper.db("select image_url from photos where zillowid = ").addVariable(listingMap.get("zillowID")).collectStrings("image_url");
		}
		
	};
	
	private String selectList = "zillowID,zillowlink,useCode,lotsize,finishedSQFT,bathrooms,bedrooms,zip,city," +
			"state,lat,lng,street,price,yearBuilt,yearUpdated,numFloors,basement,roof,view,parkingType," +
			"heatingSources,heatingSystem,appliances,floorCovering,rooms,homeDescription,neighborhood," +
			"schoolDistrict,elementarySchool,middleSchool,type,status,mls,lastUpdateDate,lag";
	
	@Override
	public void perform(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/json");
		try {
			List<String> photosArray = StmtHelper.db("select "+selectList+" from alllist where zillowID=").addVariable(request.getParameter("zillowID")).collectIntoMaps.provideTo(getPhotos).unsafeGet();
		
		StringBuffer sb = new StringBuffer();
		sb.append("<div style=\"text-align:center;width:700px;height:600px;\" id=\"detailreport\">");
		sb.append("<div style=\"width:600px;text-align:left;margin:auto;\">");
		sb.append("<div style=\"float:left;margin-top:15px;\" id=\"photoslider\">" +
				"<div class=\"scrollable\">" +
				"<div class=\"items\">");
		if(photosArray.size()<1)
			sb.append("<div class=\"item\"><img style=\"height:200px;\" src=\""+Photo.getNoPhotoURL()+"\"></div>");
		for(int i=0;i<photosArray.size();i++)
			sb.append("<div class=\"item\"><img style=\"height:200px;\" src=\""+photosArray.get(i)+"\"></div>");
		sb.append("</div>" +
				"</div>");
		if(photosArray.size()>1)
			sb.append("<div id=\"action\"><a class=\"prev\">prev</a><a class=\"next\">next</a></div>");
		sb.append("</div>");
		sb.append("<div style=\"margin:50px 0px;float:left;\" id=\"basicinfo\">");
		sb.append("<div id=\"propertyBrief\">");		
		String zillow = listingMap.get("zillowID");
		String status = listingMap.get("status")==null?"N/A":listingMap.get("status");
		String typ = listingMap.get("useCode");
		String beds = listingMap.get("bedrooms");
		String sqft = listingMap.get("finishedSQFT");
		String lotsize = listingMap.get("lotsize");
		String bathrooms = listingMap.get("bathrooms");
		String mlsnum = listingMap.get("mls")==null? listingMap.get("zillowID"): listingMap.get("mls");
		String price = listingMap.get("price")==null? "N/A":NumberFormat.getCurrencyInstance().format(Integer.parseInt(listingMap.get("price")));
		
		String yearBuilt = listingMap.get("yearBuilt")==null?"N/A":listingMap.get("yearBuilt");
		String yearUpdated = listingMap.get("yearUpdated")==null?"N/A":listingMap.get("yearUpdated");
		String numFloors = listingMap.get("numFloors")==null?"1":listingMap.get("numFloors");
		String basement = listingMap.get("basement")==null?"None":listingMap.get("basement");
		String roof = listingMap.get("roof")==null?"N/A":listingMap.get("roof");
		String view = listingMap.get("view")==null?"N/A":listingMap.get("view");
		String parkingType = listingMap.get("parkingType")==null?"N/A":listingMap.get("parkingType");
		String heatingSources = listingMap.get("heatingSources")==null?"N/A":listingMap.get("heatingSources");
		String heatingSystem = listingMap.get("heatingSystem")==null?"N/A":listingMap.get("heatingSystem");
		String appliances = listingMap.get("appliances")==null?"N/A":listingMap.get("appliances");
		String floorCovering = listingMap.get("floorCovering")==null?"N/A":listingMap.get("floorCovering");
		String rooms = listingMap.get("rooms")==null?"N/A":listingMap.get("rooms");
		String homeDescription = listingMap.get("homeDescription")==null?"N/A":listingMap.get("homeDescription");
		if(homeDescription.length()>1000)
			homeDescription=homeDescription.substring(0, 1000)+"...";
		String type = listingMap.get("rooms")==null?"N/A":listingMap.get("type");
		
		
		sb.append("<div class=\"status\"><span class=\"label\">Status:</span>"+status+"</div>");
		sb.append("<div class=\"proptype\"><span class=\"label\">Property Type:</span>"+typ+"</div>");
		sb.append("<div class=\"mlsnum\"><span class=\"label\">MLS / Zillow ID:</span>"+mlsnum+"</div>");

		
		sb.append("</div></div>");
		
		sb.append("<div style=\"margin:50px 0px;float:right;\" id=\"basicinfo\">");
		sb.append("<div id=\"propertyBrief\">");		
		sb.append("<div class=\"price\"><span class=\"label\">Price:</span>"+price+"</div>");
		sb.append("<div class=\"county\"><span class=\"label\">County:</span>Travis</div>");
		sb.append("</div></div>");
		
		sb.append("<div class=\"rowitem\">");
		sb.append("<div style=\"clear:both;float:left;margin-top:15px;\" id=\"generalinfo\">");
		sb.append("<div class=\"beds\"><span class=\"label\">Bedrooms:</span>"+beds+"</div>");
		sb.append("<div class=\"baths\"><span class=\"label\">Bathrooms:</span>"+bathrooms+"</div>");
		sb.append("<div class=\"numFloors\"><span class=\"label\">Stories:</span>"+numFloors+"</div>");
		sb.append("</div>");
		
		sb.append("<div style=\"float:right;margin-top:15px;\" id=\"sizesinfo\">");
		sb.append("<div class=\"sqft\"><span class=\"label\">Square Feet:</span>"+sqft+"</div>");
		sb.append("<div class=\"lotsize\"><span class=\"label\">Lot Size:</span>"+lotsize+"</div>");
		sb.append("</div>");
		String district = listingMap.get("schoolDistrict")==null?"N/A":listingMap.get("schoolDistrict");
		String elementary = listingMap.get("elementarySchool")==null?"N/A":listingMap.get("elementarySchool");
		String middleSchool = listingMap.get("middleSchool")==null?"N/A":listingMap.get("middleSchool");

		sb.append("<div style=\"float:left;margin-left: 100px;margin-top:15px;\" id=\"schoolinfo\">");
		sb.append("<div class=\"schoolDistrict\"><span class=\"label\">District:</span>"+district+"</div>");
		sb.append("<div class=\"elementary\"><span class=\"label\">Elementary:</span>"+elementary+"</div>");
		sb.append("<div class=\"middle\"><span class=\"label\">Middle:</span>"+middleSchool+"</div>");
		sb.append("</div>");
		sb.append("</div>");
		//yearBuilt,yearUpdated,numFloors,basement,roof,view,parkingType,
		//heatingSources,heatingSystem,appliances,floorCovering,rooms,homeDescription,type
		sb.append("<div class=\"rowitem\">");
		sb.append("<div style=\"clear:both;float:left;margin-top:15px;\" id=\"listingTypeInfo\">");
		sb.append("<div class=\"type\"><span class=\"label\">Type:</span>"+type+"</div>");
		sb.append("<div class=\"yearBuilt\"><span class=\"label\">Year Built:</span>"+yearBuilt+"</div>");
		sb.append("<div class=\"lastupdate\"><span class=\"label\">Last Updated:</span>"+yearUpdated+"</div>");
		sb.append("</div>");
		
		sb.append("<div style=\"float:right;margin-top:15px;\" id=\"interiorInfo\">");
		sb.append("<div class=\"basement\"><span class=\"label\">Basement:</span>"+basement+"</div>");
		sb.append("<div class=\"roof\"><span class=\"label\">Roof:</span>"+roof+"</div>");
		sb.append("<div class=\"view\"><span class=\"label\">View:</span>"+view+"</div>");
		sb.append("</div>");
		sb.append("</div>");
		
		sb.append("<div class=\"rowitem\">");
		sb.append("<div style=\"float:left;clear:both;margin-top:15px;\" id=\"additionalinfo\">");
		sb.append("<div class=\"parkingType\"><span class=\"label\">Parking:</span>"+parkingType+"</div>");
		sb.append("<div class=\"floorCovering\"><span class=\"label\">Floor:</span>"+floorCovering+"</div>");
		sb.append("<div class=\"rooms\"><span class=\"label\">Rooms:</span>"+rooms+"</div>");
		sb.append("</div>");
		sb.append("<div class=\"rowitem\">");
		sb.append("<div style=\"float:left;clear:both;margin-top:15px;\" id=\"electricinfo\">");
		sb.append("<div class=\"appliances\"><span class=\"label\">Appliances:</span>"+appliances+"</div>");
		sb.append("<div class=\"heatingSources\"><span class=\"label\">Heating Sources:</span>"+heatingSources+"</div>");
		sb.append("<div class=\"heatingSystem\"><span class=\"label\">Heating System:</span>"+heatingSystem+"</div>");
		sb.append("</div>");
		sb.append("</div>");
		sb.append("<div style=\"float:left;clear:both;margin-top:15px;\" >");
		sb.append("<div class=\"homeDescription\"><span class=\"label\">Home Description:</span>"+homeDescription+"</div>");
		sb.append("</div>");
		sb.append("<div id=\"\"></div>");
		sb.append("</div></div>");
		
		
		try {
			new JSONObject().put("detailreport", sb.toString()).write(response.getWriter());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}

	public static StringBuffer javaScriptOut() {
		StringBuffer sb = new StringBuffer();
		sb.append("$('#detailview .moredetails').click(" +
				"function(){" +
				"var link = $(this);$.post('"+Initialize.appURL()+"/ajaxCall.jsp?do=reports.DetailReport',{zillowID:$(this).attr('id')},function(data, textStatus, XMLHttpRequest) {"+
				"	if(data){$.fancybox(data.detailreport,{title:link.attr('title'), autodimensions:false,width:'400',height:'340',transitionIn:'elastic',transitionOut:'elastic',easingIn:'easeOutBack',easingOut:'easeInBack'});}" +
				"   $('#photoslider .scrollable').scrollable();" +
				"});"  +
				"});");
		return sb;
	}

}
